
local template = require("resty.template")
local mysql = require("lib.mysql")
local db = mysql:new()





local data = ngx.req.get_body_data()
local request_method = ngx.var.request_method

local args = nil

--获取参数的值
if "GET" == request_method then
  args = ngx.req.get_uri_args()
elseif "POST" == request_method then
  ngx.req.read_body()
  args = ngx.req.get_post_args()
end
local result={}
local url = args["url"]
local is_limit = args["is_limit"]
local limit_max = args["limit_max"]
local is_cache = args["is_cache"]
local cache_time = args["cache_time"]
local edit = args["edit"]
local limit_time = args["limit_time"]


--ngx.log(ngx.ERR,'is_cache=\"'..edit.."\"")
if  edit then
  if is_cache == nil then
    is_cache =""
  end
  if is_limit == nil then
    is_limit =""
  end
  ngx.log(ngx.ERR,'insert into   setting_URL (url,is_limit,limit_max,is_cache,cache_time,limit_time)values (\"'..url..'\",\"'..is_limit..'\",\"'..limit_max..'\",\"'..is_cache..'\",\"'..cache_time..'\",'..limit_time..')')
  local res, err, errno, sqlstate = db:query('delete from setting_URL where url=\"'..url.."\"")
  local res, err, errno, sqlstate = db:query('insert into   setting_URL (url,is_limit,limit_max,is_cache,cache_time,limit_time)values (\"'..url..'\",\"'..is_limit..'\",\"'..limit_max..'\",\"'..is_cache..'\",\"'..cache_time..'\",'..limit_time..')')
  if not res then
    ngx.log(ngx.ERR,"select error: ", err, ": ", errcode, ": ", sqlstate, ".")

  end
end



local res, err, errno, sqlstate = db:query('select * from setting_URL where url=\"'..url.."\"")
db:close()

for i, row in ipairs(res) do
  url=row["url"]
  is_limit=row["is_limit"]
  limit_max=row["limit_max"]
  is_cache=row["is_cache"]
  limit_time=row["limit_time"]

  cache_time=row.cache_time

  --for name, value in pairs(row) do
  --  result[name]=value
  ----  ngx.say("select row ", i, " : ", name, " = ", value, "")
  --end
end
ngx.log(ngx.ERR,'select * from setting_URL where url=\"'..url.."\"")
if not res then
  ngx.log(ngx.ERR,"select error: ", err, ": ", errcode, ": ", sqlstate, ".")
  res={}
  res['url']=url
  limit_max=120
  limit_time=120
  cache_time=60
end
local context = {
  url = url,
  is_limit = is_limit,
  limit_max = limit_max,
  is_cache = is_cache,
  cache_time = cache_time,
  limit_time=limit_time
}

template.render("editSettingURL.html", context)